<template>
  <div class="aui-card-list">
    <uni-list-query-card :queryShow="queryShow" @query-click="getTreeDataList()" @click="(e)=>{this.queryShow=e}" @searchReset="searchReset()">
      <el-col :span="6">
        <el-form-item :label="$t('sysOssDir.name')">
          <el-input v-model="dataForm.name" :placeholder="$t('sysOssDir.name')" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item :label="$t('sysOssDir.value')">
          <el-input v-model="dataForm.value" :placeholder="$t('sysOssDir.value')" clearable></el-input>
        </el-form-item>
      </el-col>
    </uni-list-query-card>
    <el-card shadow="never" class="aui-card-list-body">
      <vxe-toolbar :setting="{immediate:true}">
        <template v-slot:buttons>
          <el-button v-if="$hasPermission('oss:sysOssDir:save')" type="primary" @click="addOrUpdateHandle()">{{
            $t('add') }}
          </el-button>
          <el-button @click="$refs.xTree.setAllTreeExpansion(true)">{{ $t('openall') }}</el-button>
          <el-button @click="$refs.xTree.clearTreeExpand()">{{ $t('closeall') }}</el-button>
        </template>
        <template v-slot:tools>
          <el-button icon="el-icon-refresh" @click="getTreeDataList()" circle></el-button>
        </template>
      </vxe-toolbar>
      <div class="mod-oss__sysOssDir">
        <vxe-table :loading="dataListLoading" :data="dataList" ref="xTree" auto-resize class="vxe-table-element" :tree-config="{children: 'children'}">
          <vxe-table-column field="name" title="sysOssDir.name" tree-node></vxe-table-column>
          <vxe-table-column field="pnames" title="sysOssDir.pnames"></vxe-table-column>
          <vxe-table-column field="sort" title="common.sort" width="60" align="center"></vxe-table-column>
          <vxe-table-column field="treeLevel" title="common.treeLevel" width="60" align="center"></vxe-table-column>
          <vxe-table-column field="updateDate" title="common.updateDate" width="140" align="center"></vxe-table-column>
          <vxe-table-column title="handle" fixed="right" align="center" width="150">
            <template slot-scope="scope">
              <el-button v-if="$hasPermission('oss:sysOssDir:update')" type="text" icon="el-icon-edit" size="small" @click="addOrUpdateHandle(scope.row.id)">{{
                $t('update') }}
              </el-button>
              <el-button v-if="$hasPermission('oss:sysOssDir:delete')" type="text" icon="el-icon-delete" size="small" @click="deleteHandle(scope.row.id)">{{
                $t('delete') }}
              </el-button>
            </template>
          </vxe-table-column>
        </vxe-table>
        <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getTreeDataList"></add-or-update>
      </div>
    </el-card>
  </div>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './sysOssDir-add-or-update'
import { treeDataTranslate } from '@/utils/index'

export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListIsPage: false,
        deleteURL: '/oss/sysOssDir',
        getTreeDataListURL: '/oss/sysOssDir/list',
        treeType: true
      },
      dataForm: {
        id: '',
        pid: '',
        name: '',
        value: '',
        pids: '',
        pnames: '',
        creator: ''
      }
    }
  },
  components: {
    AddOrUpdate
  },
  methods: {
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.getTreeCompList(id).then(res => {
        this.$refs.addOrUpdate.parentTreeData = res
        this.$nextTick(() => {
          this.$refs.addOrUpdate.dataForm.id = id
          this.$refs.addOrUpdate.init()
        })
      })
    },
    // 父级树组件
    getTreeCompList (id) {
      return new Promise(resolve => {
        this.$http.get(
          this.mixinViewModuleOptions.getTreeDataListURL,
          {
            params: {
              id_ne: id,
              pid_ne: id
            }
          }
        ).then(({ data: res }) => {
          if (res.code !== 0) {
            return
          }
          resolve(treeDataTranslate(res.data, 'id', 'pid'))
        }).catch(err => {
          return this.$message.error(err)
        })
      })
    }
  }
}
</script>
